package com.hqd.ch03.v24.aop.support;

import com.hqd.ch03.v24.aop.SpringImitationProxy;
import com.hqd.ch03.v24.utils.ClassUtils;

import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public abstract class AopUtils {
    public static boolean isJdkDynamicProxy(Object object) {
        return (object instanceof SpringImitationProxy && Proxy.isProxyClass(object.getClass()));
    }

    public static boolean isCglibProxy(Object object) {
        return (object instanceof SpringImitationProxy &&
                object.getClass().getName().contains(ClassUtils.CGLIB_CLASS_SEPARATOR));
    }

    public static boolean isEqualsMethod(Method method) {
        if (method == null) {
            return false;
        }
        if (method.getParameterCount() != 1) {
            return false;
        }
        if (!method.getName().equals("equals")) {
            return false;
        }
        return method.getParameterTypes()[0] == Object.class;
    }

    public static boolean isHashCodeMethod(Method method) {
        return method != null && method.getParameterCount() == 0 && method.getName().equals("hashCode");
    }

    public static boolean isToStringMethod(Method method) {
        return (method != null && method.getParameterCount() == 0 && method.getName().equals("toString"));
    }

    public static boolean isFinalizeMethod(Method method) {
        return (method != null && method.getName().equals("finalize") &&
                method.getParameterCount() == 0);
    }
}
